import { TODO_ADD, TODO_DEL_BY_ID, TODO_SET_ALL, TODO_UPDATE_TYPE } from './actionType';
import { TODO_UPDATE_BY_ID } from './actionType';

export const delAction = (id) => ({
  type: TODO_DEL_BY_ID,
  payload: id,
});

export const updateAction = (id) => ({
  type: TODO_UPDATE_BY_ID,
  payload: id,
});

function loadNewTodoAPI() {
  const newTodo = {
    task: '异步返回的任务',
    id: Date.now(),
    isDone: false,
  };
  console.log('开始请求');
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('请求成功，2秒后,数据返回');
      resolve(newTodo);
    }, 2000);
  });
}

export const addAction = (newTask) => {
  return async (dispatch) => {
    const res = await loadNewTodoAPI();
    dispatch({ type: TODO_ADD, payload: res });
  };
};

export const updateTypeAction = (type) => ({
  type: TODO_UPDATE_TYPE,
  payload: type,
});

export const setAll = (checked) => ({
  type: TODO_SET_ALL,
  payload: checked,
});
